package at.socialconference.backend.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import at.socialconference.backend.persistence.DBController;

@Entity
@Table(name = "t_user")
public class User implements Insertable {

	// Low Level: min infos

	public static final int LEVEL_LOW = 0;
	public static final int LEVEL_MEDIUM = 1;
	public static final int LEVEL_HIGH = 2;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "userid")
	private Integer id;

	@Column(name = "firstname")
	private String firstname;

	@Column(name = "lastname")
	private String lastname;

	@Column(name = "gender")
	private String gender;

	@Column(name = "phone")
	private String phone;

	@Column(name = "title")
	private String title;

	@Column(name = "webpage")
	private String webpage;

	@Column(name = "dblp")
	private String dblp;

	@Column(name = "city")
	private String city;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "country")
	private Country country;

	@Column(name = "address")
	private String address;

	@Column(name = "zip")
	private String zip;

	@Column(name = "mail")
	private String mail;

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "t_user_topic", joinColumns = { @JoinColumn(name = "userid") }, inverseJoinColumns = { @JoinColumn(name = "topicid") })
	private Set<Topic> fieldOfInterests = new HashSet<Topic>();

	// @ManyToMany()
	// @JoinTable(name = "t_relations", joinColumns = { @JoinColumn(name =
	// "userid_a") }, inverseJoinColumns = { @JoinColumn(name = "userid_b") })
	// private List<User> relations = new ArrayList<User>();

	@Column(name = "tw_access_token")
	private String tw_access_token;

	@Column(name = "tw_access_token_secret")
	private String tw_access_secret;

	public User() {

	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getWebpage() {
		return webpage;
	}

	public void setWebpage(String webpage) {
		this.webpage = webpage;
	}

	public String getDblp() {
		return dblp;
	}

	public void setDblp(String dblp) {
		this.dblp = dblp;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public void setFieldOfInterests(Set<Topic> fieldOfInterests) {
		this.fieldOfInterests = fieldOfInterests;
	}

	public Set<Topic> getFieldOfInterests() {
		return fieldOfInterests;
	}

	public void addTopic(Topic topic) {
		fieldOfInterests.add(topic);
	}

	// public void setRelations(List<User> relations) {
	// this.relations = relations;
	// }
	// public List<User> getRelations() {
	// return relations;
	// }
	// public void addFriend(User user){
	// relations.add(user);
	// }
	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getMail() {
		return mail;
	}

	public void setTw_access_token(String tw_access_token) {
		this.tw_access_token = tw_access_token;
	}

	public String getTw_access_token() {
		return tw_access_token;
	}

	public void setTw_access_secret(String tw_access_secret) {
		this.tw_access_secret = tw_access_secret;
	}

	public String getTw_access_secret() {
		return tw_access_secret;
	}

	/**
	 * Creates an interUser Object
	 * 
	 * @param level
	 *            TrustLevel
	 * @return Filtered interUser Object
	 */
	public at.socialconference.inter.model.User generateInterUser(int level) {
		String countryname = country == null ? "" : country.getName();
		at.socialconference.inter.model.User user = new at.socialconference.inter.model.User(id, firstname, lastname, gender, phone, title,
				webpage, dblp, city, countryname, address, zip, mail, null, tw_access_token, tw_access_secret);
		for (Topic t : fieldOfInterests)
			user.addTopic(t.getTopic());

		switch (level) {
		case User.LEVEL_LOW:
			user.setPhone(null);
			user.setAddress(null);
			user.setCity(null);
			user.setZip(null);
			user.setMail(null);
			user.setTw_access_token(null);
			user.setTw_access_secret(null);
			break;
		case User.LEVEL_MEDIUM:
			user.setContacts(null);
			user.setTw_access_token(null);
			user.setTw_access_secret(null);
			break;
		case User.LEVEL_HIGH:
			break;
		default:
			break;
		}
		user.setSecurityLevel(level);
		return user;
	}

	/**
	 * Constructor (Wrapper)
	 * 
	 * @param user
	 *            InterModelUser
	 */
	public User(at.socialconference.inter.model.User user) {
		this.setId(user.getId());
		this.setFirstname(user.getFirstname());
		this.setLastname(user.getLastname());
		this.setGender(user.getGender());
		this.setPhone(user.getPhone());
		this.setTitle(user.getTitle());
		this.setWebpage(user.getWebpage());
		this.setDblp(user.getDblp());
		this.setCity(user.getCity());
		this.setDblp(user.getDblp());
		this.setZip(user.getZip());
		this.setAddress(user.getAddress());
		this.setMail(user.getMail());
		this.setTw_access_token(user.getTw_access_token());
		this.setTw_access_secret(user.getTw_access_secret());
		Country c = DBController.getCountry(user.getCountry());
		this.setCountry(c);
		this.setAddress(user.getAddress());
	}
}
